{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 发送HTTP请求：\n",
    "\n",
    "`requests`包主要用于与Web服务器进行通信，最常见的方式是发送HTTP请求。它提供了几个主要的方法来发送不同类型的请求：\n",
    "\n",
    "1. **GET请求**：使用`requests.get()`方法发送GET请求，从服务器获取资源。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'http://www.nows.fun/'\n",
    "response = requests.get(url)\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "2. **POST请求**：使用`requests.post()`方法发送POST请求，向服务器提交数据。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(url, data={'key': 'value'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "3. **其他请求方法**：`requests`还支持其他HTTP请求方法，如PUT、DELETE等，对应的方法名为`put()`、`delete()`等。\n",
    "\n",
    "### 处理响应：\n",
    "\n",
    "一旦发送了请求，`requests`会返回一个响应对象，您可以使用它来访问响应的内容、状态码等信息。\n",
    "\n",
    "1. **获取响应内容**：使用`.text`属性获取响应的文本内容，或使用`.content`属性获取原始的字节内容。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ï»¿<!DOCTYPE html>\n",
      "<!-- saved from http://nows.fun/ -->\n",
      "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n",
      "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">\n",
      "\n",
      "\t<title>æ¯é¸¡æ±¤ - å£®å£«å¯è¦æ¥ä¸ç¢ï¼</title>\n",
      "\t<meta name=\"description\" content=\"æä»¬ç²¾å¿ç¬å¶äºæå±ä»¥æ¥ææ¯1000å¤æ¡ç»å¸æ¯é¸¡æ±¤,å¥å¥âæ²»æâäººå¿! åªä¸ºäºå¸®ä½ æ´å¥½ççæ¸äººçè®¤è¯èªå·±ï¼ç´é¢ç°å®,ç´é¢æ¨æ·¡çäººç,ä¸ç»ä½ å±å¿,ä¸ç»ä½ æ°è,åä¸æ ¹é­ççæ½ä½ ä¸ä¸,ä½¿ä½ æ¸é,ç¥éè¿ä¸ªä¸çåä½ èªå·±æçå®çä¸é¢,æ¯éå°å¹´ææºéçå¿å¤å®å¸ã\">\n",
      "\t<meta name=\"keywords\" content=\"é¸¡æ±¤,æ¯é¸¡æ±¤,é¦é¸¡æ±¤\">\n",
      "\t<meta http-equiv=\"Cache-Control\" content=\"no-siteapp\">\n",
      "\t<meta name=\"author\" content=\"yatang@126.com\" />\n",
      "\t<meta property=\"og:title\" content=\"æ¯é¸¡æ±¤ - å£®å£«å¯è¦æ¥ä¸ç¢ï¼\" />\n",
      "\t<meta property=\"og:url\" content=\"http://www.nows.fun/\" />\n",
      "\t<meta property=\"og:image\" content=\"http://www.nows.fun/icon.png\" />\n",
      "\t<meta property=\"og:site_name\" content=\"æ¯é¸¡æ±¤ - å£®å£«å¯è¦æ¥ä¸ç¢ï¼\" />\n",
      "\t<meta property=\"og:description\" content=\"æä»¬ç²¾å¿ç¬å¶äºæå±ä»¥æ¥ææ¯1000å¤æ¡ç»å¸æ¯é¸¡æ±¤,å¥å¥âæ²»æâäººå¿! åªä¸ºäºå¸®ä½ æ´å¥½ççæ¸äººçè®¤è¯èªå·±ï¼ç´é¢ç°å®,ç´é¢æ¨æ·¡çäººç,ä¸ç»ä½ å±å¿,ä¸ç»ä½ æ°è,åä¸æ ¹é­ççæ½ä½ ä¸ä¸,ä½¿ä½ æ¸é,ç¥éè¿ä¸ªä¸çåä½ èªå·±æçå®çä¸é¢,æ¯éå°å¹´ææºéçå¿å¤å®å¸ã\"/>\n",
      "\t<link rel=\"icon\" href=\"/favicon.ico\" type=\"image/x-icon\" id=\"page_favionc\">\n",
      "\t<link href=\"./img/min.css\" rel=\"stylesheet\">\n",
      "\t<link rel=\"alternate icon\" type=\"image/png\" href=\"icon.png\">\n",
      "\n",
      "</head>\n",
      "<body>\n",
      "<div class=\"top-wrap\" style=\"position: absolute; top: 1vh;width: 100%;z-index: 999\"> \n",
      "<div class=\"container\">\n",
      "\t<div class=\"row\" style=\"margin-top: 30px;\">\n",
      "\t    <div class=\"col\">\n",
      "\t      <img src=\"./img/logo.png\">\n",
      "\t    </div>\n",
      "\t    <div class=\"col\">\n",
      "\t    \t<div class=\"float-right\" style=\"padding-top: 0px;\">\n",
      "\t    \t\t<a class=\"btn btn-primary btn-filled btn-xs\" href=\"https://github.com/egotong/nows\" >å¼æº</a>\n",
      "\t    \t</div>\n",
      "\t    </div>\n",
      "\t\t</div>\n",
      "</div>\n",
      "</div>\n",
      "\n",
      "<div class=\"main-wrapper\" style=\"position: relative; top: -6vh;\">\n",
      "<div class=\"container main-sentence justify-content-center text-center\">\n",
      "\t\n",
      "\t\t\t\t\t\t\n",
      "\t\t\t\n",
      "\t\t\t\t\t\n",
      "\t\t\t\t\t\t\n",
      "\t\t\t\t\t\t<span id=\"sentence\" style=\"font-size: 2rem;\">é£äºäººäººç¾¡æçç¤¾ä¼ç²¾è±ï¼å¶å®è¿å¾ä¸å¦ä½ æ³è±¡é£æ ·å¥½ãä½è¯å®æ¯ä½ å¼ºå¾å¤çå¤ã</span>\n",
      "\t\t\t\t            \t\n",
      "\n",
      "\n",
      "</div>\n",
      "</div>\n",
      "\n",
      "<div class=\"foot-1\" style=\"position: absolute; bottom: 7vh;width: 100%;\">\n",
      "\t<div class=\"container\">\n",
      "\t\t<div class=\"row\">\n",
      "\t\t\t<div class=\"col text-center\">\n",
      "\t            <p class=\"lead text\">æ¯ä»å® æç´¢ 725582234 é¢ä¸ªå°çº¢åå§</p>\n",
      "\n",
      "\n",
      "\t            <span class=\"btn btn-primary btn-filled btn-xs\"><a class=\"btn btn-primary btn-filled btn-xs\" href=\"http://www.nows.fun\" >www.nows.fun</a></span>\n",
      "\t    </div>\n",
      "  \t\t</div>\n",
      "\t</div>\n",
      "</div>\n",
      "\n",
      "<div style=\"display:none;\">\n",
      "<script type=\"text/javascript\" src=\"//api.tongjiniao.com/c?_=634151056438480896\" async></script>\n",
      "</div>\n",
      "\n",
      "</body>\n",
      "</html>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(response.text)\n",
    "print(type(response.text))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xef\\xbb\\xbf<!DOCTYPE html>\\n<!-- saved from http://nows.fun/ -->\\n<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">\\n\\n\\t<title>\\xe6\\xaf\\x92\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4 - \\xe5\\xa3\\xae\\xe5\\xa3\\xab\\xe5\\x8f\\xaf\\xe8\\xa6\\x81\\xe6\\x9d\\xa5\\xe4\\xb8\\x80\\xe7\\xa2\\x97\\xef\\xbc\\x81</title>\\n\\t<meta name=\"description\" content=\"\\xe6\\x88\\x91\\xe4\\xbb\\xac\\xe7\\xb2\\xbe\\xe5\\xbf\\x83\\xe7\\x86\\xac\\xe5\\x88\\xb6\\xe4\\xba\\x86\\xe6\\x9c\\x89\\xe5\\xb1\\x8e\\xe4\\xbb\\xa5\\xe6\\x9d\\xa5\\xe6\\x9c\\x80\\xe6\\xaf\\x921000\\xe5\\xa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\\xb8\\x96\\xe7\\x95\\x8c\\xe5\\x92\\x8c\\xe4\\xbd\\xa0\\xe8\\x87\\xaa\\xe5\\xb7\\xb1\\xe6\\x9c\\x80\\xe7\\x9c\\x9f\\xe5\\xae\\x9e\\xe7\\x9a\\x84\\xe4\\xb8\\x80\\xe9\\x9d\\xa2,\\xe6\\x98\\xaf\\xe9\\x9d\\x92\\xe5\\xb0\\x91\\xe5\\xb9\\xb4\\xe6\\x89\\x8b\\xe6\\x9c\\xba\\xe9\\x87\\x8c\\xe7\\x9a\\x84\\xe5\\xbf\\x85\\xe5\\xa4\\x87\\xe5\\xae\\x9d\\xe5\\x85\\xb8\\xe3\\x80\\x82\">\\n\\t<meta name=\"keywords\" content=\"\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4,\\xe6\\xaf\\x92\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4,\\xe9\\xa6\\x8a\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4\">\\n\\t<meta http-equiv=\"Cache-Control\" content=\"no-siteapp\">\\n\\t<meta name=\"author\" content=\"yatang@126.com\" />\\n\\t<meta property=\"og:title\" content=\"\\xe6\\xaf\\x92\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4 - \\xe5\\xa3\\xae\\xe5\\xa3\\xab\\xe5\\x8f\\xaf\\xe8\\xa6\\x81\\xe6\\x9d\\xa5\\xe4\\xb8\\x80\\xe7\\xa2\\x97\\xef\\xbc\\x81\" />\\n\\t<meta property=\"og:url\" content=\"http://www.nows.fun/\" />\\n\\t<meta property=\"og:image\" content=\"http://www.nows.fun/icon.png\" />\\n\\t<meta property=\"og:site_name\" content=\"\\xe6\\xaf\\x92\\xe9\\xb8\\xa1\\xe6\\xb1\\xa4 - \\xe5\\xa3\\xae\\xe5\\xa3\\xab\\xe5\\x8f\\xaf\\xe8\\xa6\\x81\\xe6\\x9d\\xa5\\xe4\\xb8\\x80\\xe7\\xa2\\x97\\xef\\xbc\\x81\" />\\n\\t<meta property=\"og:description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\\x9b\\xe7\\x9a\\x84\\xe6\\x8a\\xbd\\xe4\\xbd\\xa0\\xe4\\xb8\\x80\\xe4\\xb8\\x8b,\\xe4\\xbd\\xbf\\xe4\\xbd\\xa0\\xe6\\xb8\\x85\\xe9\\x86\\x92,\\xe7\\x9f\\xa5\\xe9\\x81\\x93\\xe8\\xbf\\x99\\xe4\\xb8\\xaa\\xe4\\xb8\\x96\\xe7\\x95\\x8c\\xe5\\x92\\x8c\\xe4\\xbd\\xa0\\xe8\\x87\\xaa\\xe5\\xb7\\xb1\\xe6\\x9c\\x80\\xe7\\x9c\\x9f\\xe5\\xae\\x9e\\xe7\\x9a\\x84\\xe4\\xb8\\x80\\xe9\\x9d\\xa2,\\xe6\\x98\\xaf\\xe9\\x9d\\x92\\xe5\\xb0\\x91\\xe5\\xb9\\xb4\\xe6\\x89\\x8b\\xe6\\x9c\\xba\\xe9\\x87\\x8c\\xe7\\x9a\\x84\\xe5\\xbf\\x85\\xe5\\xa4\\x87\\xe5\\xae\\x9d\\xe5\\x85\\xb8\\xe3\\x80\\x82\"/>\\n\\t<link rel=\"icon\" href=\"/favicon.ico\" type=\"image/x-icon\" id=\"page_favionc\">\\n\\t<link href=\"./img/min.css\" rel=\"stylesheet\">\\n\\t<link rel=\"alternate icon\" type=\"image/png\" href=\"icon.png\">\\n\\n</head>\\n<body>\\n<div class=\"top-wrap\" style=\"position: absolute; top: 1vh;width: 100%;z-index: 999\"> \\n<div class=\"container\">\\n\\t<div class=\"row\" style=\"margin-top: 30px;\">\\n\\t    <div class=\"col\">\\n\\t      <img src=\"./img/logo.png\">\\n\\t    </div>\\n\\t    <div class=\"col\">\\n\\t    \\t<div class=\"float-right\" style=\"padding-top: 0px;\">\\n\\t    \\t\\t<a class=\"btn btn-primary btn-filled btn-xs\" href=\"https://github.com/egotong/nows\" >\\xe5\\xbc\\x80\\xe6\\xba\\x90</a>\\n\\t    \\t</div>\\n\\t    </div>\\n\\t\\t</div>\\n</div>\\n</div>\\n\\n<div class=\"main-wrapper\" style=\"position: relative; top: -6vh;\">\\n<div class=\"container main-sentence justify-content-center text-center\">\\n\\t\\n\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t<span id=\"sentence\" style=\"font-size: 2rem;\">\\xe9\\x82\\xa3\\xe4\\xba\\x9b\\xe4\\xba\\xba\\xe4\\xba\\xba\\xe7\\xbe\\xa1\\xe6\\x85\\x95\\xe7\\x9a\\x84\\xe7\\xa4\\xbe\\xe4\\xbc\\x9a\\xe7\\xb2\\xbe\\xe8\\x8b\\xb1\\xef\\xbc\\x8c\\xe5\\x85\\xb6\\xe5\\xae\\x9e\\xe8\\xbf\\x87\\xe5\\xbe\\x97\\xe4\\xb8\\x8d\\xe5\\xa6\\x82\\xe4\\xbd\\xa0\\xe6\\x83\\xb3\\xe8\\xb1\\xa1\\xe9\\x82\\xa3\\xe6\\xa0\\xb7\\xe5\\xa5\\xbd\\xe3\\x80\\x82\\xe4\\xbd\\x86\\xe8\\x82\\xaf\\xe5\\xae\\x9a\\xe6\\xaf\\x94\\xe4\\xbd\\xa0\\xe5\\xbc\\xba\\xe5\\xbe\\x97\\xe5\\xa4\\x9a\\xe7\\x9a\\x84\\xe5\\xa4\\x9a\\xe3\\x80\\x82</span>\\n\\t\\t\\t\\t            \\t\\n\\n\\n</div>\\n</div>\\n\\n<div class=\"foot-1\" style=\"position: absolute; bottom: 7vh;width: 100%;\">\\n\\t<div class=\"container\">\\n\\t\\t<div class=\"row\">\\n\\t\\t\\t<div class=\"col text-center\">\\n\\t            <p class=\"lead text\">\\xe6\\x94\\xaf\\xe4\\xbb\\x98\\xe5\\xae\\x9d \\xe6\\x90\\x9c\\xe7\\xb4\\xa2 725582234 \\xe9\\xa2\\x86\\xe4\\xb8\\xaa\\xe5\\xb0\\x8f\\xe7\\xba\\xa2\\xe5\\x8c\\x85\\xe5\\x90\\xa7</p>\\n\\n\\n\\t            <span class=\"btn btn-primary btn-filled btn-xs\"><a class=\"btn btn-primary btn-filled btn-xs\" href=\"http://www.nows.fun\" >www.nows.fun</a></span>\\n\\t    </div>\\n  \\t\\t</div>\\n\\t</div>\\n</div>\\n\\n<div style=\"display:none;\">\\n<script type=\"text/javascript\" src=\"//api.tongjiniao.com/c?_=634151056438480896\" async></script>\\n</div>\\n\\n</body>\\n</html>'\n",
      "<class 'bytes'>\n"
     ]
    }
   ],
   "source": [
    "print(response.content)\n",
    "print(type(response.content))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html>\n",
      "<!-- saved from http://nows.fun/ -->\n",
      "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n",
      "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">\n",
      "\n",
      "\t<title>毒鸡汤 - 壮士可要来一碗！</title>\n",
      "\t<meta name=\"description\" content=\"我们精心熬制了有屎以来最毒1000多条经典毒鸡汤,句句“治愈”人心! 只为了帮你更好的看清人生认识自己，直面现实,直面惨淡的人生,不给你励志,不给你慰藉,像一根鞭猛的抽你一下,使你清醒,知道这个世界和你自己最真实的一面,是青少年手机里的必备宝典。\">\n",
      "\t<meta name=\"keywords\" content=\"鸡汤,毒鸡汤,馊鸡汤\">\n",
      "\t<meta http-equiv=\"Cache-Control\" content=\"no-siteapp\">\n",
      "\t<meta name=\"author\" content=\"yatang@126.com\" />\n",
      "\t<meta property=\"og:title\" content=\"毒鸡汤 - 壮士可要来一碗！\" />\n",
      "\t<meta property=\"og:url\" content=\"http://www.nows.fun/\" />\n",
      "\t<meta property=\"og:image\" content=\"http://www.nows.fun/icon.png\" />\n",
      "\t<meta property=\"og:site_name\" content=\"毒鸡汤 - 壮士可要来一碗！\" />\n",
      "\t<meta property=\"og:description\" content=\"我们精心熬制了有屎以来最毒1000多条经典毒鸡汤,句句“治愈”人心! 只为了帮你更好的看清人生认识自己，直面现实,直面惨淡的人生,不给你励志,不给你慰藉,像一根鞭猛的抽你一下,使你清醒,知道这个世界和你自己最真实的一面,是青少年手机里的必备宝典。\"/>\n",
      "\t<link rel=\"icon\" href=\"/favicon.ico\" type=\"image/x-icon\" id=\"page_favionc\">\n",
      "\t<link href=\"./img/min.css\" rel=\"stylesheet\">\n",
      "\t<link rel=\"alternate icon\" type=\"image/png\" href=\"icon.png\">\n",
      "\n",
      "</head>\n",
      "<body>\n",
      "<div class=\"top-wrap\" style=\"position: absolute; top: 1vh;width: 100%;z-index: 999\"> \n",
      "<div class=\"container\">\n",
      "\t<div class=\"row\" style=\"margin-top: 30px;\">\n",
      "\t    <div class=\"col\">\n",
      "\t      <img src=\"./img/logo.png\">\n",
      "\t    </div>\n",
      "\t    <div class=\"col\">\n",
      "\t    \t<div class=\"float-right\" style=\"padding-top: 0px;\">\n",
      "\t    \t\t<a class=\"btn btn-primary btn-filled btn-xs\" href=\"https://github.com/egotong/nows\" >开源</a>\n",
      "\t    \t</div>\n",
      "\t    </div>\n",
      "\t\t</div>\n",
      "</div>\n",
      "</div>\n",
      "\n",
      "<div class=\"main-wrapper\" style=\"position: relative; top: -6vh;\">\n",
      "<div class=\"container main-sentence justify-content-center text-center\">\n",
      "\t\n",
      "\t\t\t\t\t\t\n",
      "\t\t\t\n",
      "\t\t\t\t\t\n",
      "\t\t\t\t\t\t\n",
      "\t\t\t\t\t\t<span id=\"sentence\" style=\"font-size: 2rem;\">那些人人羡慕的社会精英，其实过得不如你想象那样好。但肯定比你强得多的多。</span>\n",
      "\t\t\t\t            \t\n",
      "\n",
      "\n",
      "</div>\n",
      "</div>\n",
      "\n",
      "<div class=\"foot-1\" style=\"position: absolute; bottom: 7vh;width: 100%;\">\n",
      "\t<div class=\"container\">\n",
      "\t\t<div class=\"row\">\n",
      "\t\t\t<div class=\"col text-center\">\n",
      "\t            <p class=\"lead text\">支付宝 搜索 725582234 领个小红包吧</p>\n",
      "\n",
      "\n",
      "\t            <span class=\"btn btn-primary btn-filled btn-xs\"><a class=\"btn btn-primary btn-filled btn-xs\" href=\"http://www.nows.fun\" >www.nows.fun</a></span>\n",
      "\t    </div>\n",
      "  \t\t</div>\n",
      "\t</div>\n",
      "</div>\n",
      "\n",
      "<div style=\"display:none;\">\n",
      "<script type=\"text/javascript\" src=\"//api.tongjiniao.com/c?_=634151056438480896\" async></script>\n",
      "</div>\n",
      "\n",
      "</body>\n",
      "</html>\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "response_content = response.content.decode('utf-8')\n",
    "print(response_content)\n",
    "print(type(response_content))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "2. **解析JSON响应**：如果响应是JSON格式的数据，可以使用`.json()`方法将其解析为Python字典。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "3. **获取状态码**：使用`.status_code`属性获取响应的HTTP状态码。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.status_code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "4. **其他响应信息**：`requests`还提供了许多其他属性和方法，如`.headers`获取响应头信息、`.url`获取请求的URL等。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response.headers\n",
    "response.url"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 其他功能：\n",
    "\n",
    "除了发送请求和处理响应外，`requests`还提供了许多其他功能，使得与Web服务器进行交互更加灵活和方便：\n",
    "\n",
    "1. **请求参数**：可以使用`params`参数将查询字符串参数添加到GET请求中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.get(url, params={'key': 'value'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "2. **请求头设置**：可以使用`headers`参数设置请求头信息，如User-Agent、Authorization等。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {'User-Agent': 'Mozilla/5.0'}\n",
    "response = requests.get(url, headers=headers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "3. **异常处理**：`requests`会自动处理常见的HTTP错误，但您也可以通过捕获`requests.exceptions.RequestException`来处理请求过程中的异常情况。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    response = requests.get(url)\n",
    "    response.raise_for_status()\n",
    "except requests.exceptions.RequestException as e:\n",
    "    print(\"请求发生错误:\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "4. **会话对象**：可以使用`Session`对象来保持会话状态，发送多个请求共享同一会话。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session = requests.Session()\n",
    "response1 = session.get(url1)\n",
    "response2 = session.get(url2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "通过这些基本方法和功能，学生们可以轻松地使用`requests`包与Web服务器进行交互，从而实现数据的抓取、提交和处理。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
